package hidra.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jmetal.problems.adaboost.Config;
import jmetal.util.JMException;
import hidra.jmetal.core.SolutionSet;

public class ClassifierWriter {
	
	private SolutionSet solutions;
	private int simulation;
	
	public ClassifierWriter(SolutionSet solutionSet, int simulation)
	{
		this.solutions = solutionSet;
		this.simulation = simulation;
	}
	
	public void saveClassifiers() throws IOException, JMException
	{
		int solutionSize = solutions.size();
		File file = new File("classificadores" +Config.NUMBER_OF_CLASSIFIERS + "/simulacao"+simulation);
		file.mkdir();
		for(int i = 0; i < solutionSize; i++)
		{
			file = new File("classificadores/simulacao"+simulation+"/classifier-"+i);
			if(file.exists())
				file.delete();
			FileWriter fw = new FileWriter(file);
			String classifier = "";
			classifier = solutions.get(i).getObjective(0) + "\t" + solutions.get(i).getObjective(1) + "\n";
			for(int j = 0; j < solutions.get(i).getDecisionVariables().length; j++)
			{
				
				if(solutions.get(i).getDecisionVariables()[j].getLowerBound()==0.0 &&solutions.get(i).getDecisionVariables()[j].getUpperBound()==0.0 )
				{
					solutions.get(i).getDecisionVariables()[j].setValue(0.0); 
				}
				classifier += solutions.get(i).getDecisionVariables()[j].getValue() + "\t";
			}
			System.out.println("\nObjetivos 1, 2 do Classificador " + i+"\n"+classifier);
			fw.write(classifier);
			fw.close();
		}
	}
	
}
